package soap.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import soap.pojo.ImageDto;
import soap.pojo.MyUser;
import soap.service.ImageDtoMongoService;
import soap.service.JDInfoEsService;
import soap.utils.TestUtils;

import java.io.IOException;
import java.util.*;

/**
 * Created by ZhangPY on 2020/12/20
 * Belong Organization OVERUN-9299
 * overun9299@163.com
 * Explain: 爬取京东数据等相关
 */
@RestController
@RequestMapping("/jd")
public class JDInfoEsController {

    @Autowired
    private JDInfoEsService jdInfoEsService;

    @Autowired
    private ImageDtoMongoService imageDtoMongoService;

    @Autowired
    private TestUtils testUtils;


    @Value("${server.port}")
    private String portId;

    @GetMapping("/getPortId")
    public String getPortId() {
        return portId;
    }

    /**
     * 获取京东数据，并打入es
     * @param keyWord
     * @return
     */
    @GetMapping("/getJDInfoToES")
    public Boolean getJDInfoToES(String keyWord) {
        return jdInfoEsService.getJDInfoToES(keyWord);
    }

    /**
     * 搜索京东数据
     * @param keyWord
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/searchJDGoods")
    public String searchJDGoods(String keyWord , Integer pageNo , Integer pageSize) {
        return jdInfoEsService.searchJDGoods(keyWord , pageNo , pageSize);
    }

    @GetMapping("/getImageByKeyWorld")
    public String getImageByKeyWorld(String keyWord) throws IOException, InterruptedException {

        System.out.println("搜索key为" + keyWord);

        Set<ImageDto> jdImg = testUtils.getJDImg(keyWord, 0);

        return JSONObject.toJSONString(jdImg);
    }

    @GetMapping("/getImageByKeyFromMongo")
    public String getImageByKeyFromMongo(String keyWord) throws IOException, InterruptedException {

        System.out.println("搜索key为" + keyWord);

        List<ImageDto> jdImg = imageDtoMongoService.getImageByKeyFromMongo(keyWord);

        return JSONObject.toJSONString(jdImg);
    }

}
